//
//  MTMineWebViewController.swift
//  MT
//
//  Created by ooops on 2020/3/13.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import WebKit
import MTCategory

protocol MTMineWebViewControllerDelegate: NSObjectProtocol {
    
    func navigation()
    
}

class MTMineWebViewController: BaseViewController {

    var webView: WKWebView!
    
    weak var delegate: MTMineWebViewControllerDelegate?
    
    override func loadView() {
        
        let conf = WKWebViewConfiguration()
        conf.userContentController = WKUserContentController()
        conf.preferences.javaScriptEnabled = true
        conf.selectionGranularity = WKSelectionGranularity.character
        conf.allowsInlineMediaPlayback = true
        
        webView = WKWebView(frame: .zero, configuration: conf)  //.zero
        //禁止顶部下拉 和 底部上拉效果
        webView.scrollView.bounces = false
        //解决全屏播放视频 状态栏闪现导致的底部白条  never:表示不计算内边距
        webView.scrollView.contentInsetAdjustmentBehavior = .never
        
        view = webView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if selectValue == 0 {
            title = "用户协议"
            let fileURL =  Bundle.main.url(forResource: "userAgreement", withExtension: "html")
            webView.loadFileURL(fileURL!,allowingReadAccessTo:Bundle.main.bundleURL);
        } else if selectValue == 1 {
            title = "隐私条款"
            let fileURL =  Bundle.main.url(forResource: "privacy", withExtension: "html")
            webView.loadFileURL(fileURL!,allowingReadAccessTo:Bundle.main.bundleURL);
        } else {
            title = "用户充值协议"
            //let fileURL =  Bundle.main.url(forResource: "privacy", withExtension: "html")
            if let url = URL(string: "https://mygs-api.moveme.cn/recharge_privacy.html"), let urlR: URLRequest = URLRequest(url: url) {
                webView.load(urlR)
            }
        }
        
        _back()
        
        self.navigationController?.mt_enableFullScreenPop(isEnabled: false)
        self.navigationController?.interactivePopGestureRecognizer?.delegate = nil
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        self.delegate?.navigation()
    }
    
    private func _back() {
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "nav_back"), style: .done, target: self, action: #selector(_pop))
    }
    
    @objc func _pop() {
        super.pop()
        self.delegate?.navigation()
    }
    
    var selectValue: Int?

}
